package model

import (
	"time"
)

// OperationLog 操作日志
type OperationLog struct {
	BaseID            //主键
	UserId     string ` gorm:"type:varchar(32)"`                           //用户主键
	User       *User  `gorm:"foreignkey:UserId;association_foreignkey:id"` //用户实体
	LogTime    int64  //日志创建时间
	LogTimeStr string //创建日期的字符串形式
	LogContent string //日志内容
}

func NewOperationLog(userId string, logContent string) *OperationLog {
	now := time.Now()
	return &OperationLog{
		UserId:     userId,
		LogTime:    now.Unix(),
		LogTimeStr: now.Format("2006-01-02 15:04:05"),
		LogContent: logContent,
	}
}

//type OperationLogPageSupport struct {
//	PageSupport[OperationLogListStruct]
//	StartTime string `json:"startTime"`
//	EndTime   string `json:"endTime"`
//}
//
//type OperationLogListStruct struct {
//	ID         string
//	Username   string
//	Logtime    string
//	Logcontent string
//}
//
//func (o *OperationLog) FindPage(obj ...interface{}) OperationLogPageSupport {
//	pageSupport := obj[0].(*OperationLogPageSupport)
//	var OperationLogListStructList []OperationLogListStruct
//	query := DB.Table("hndz_operation_log as log").
//		Select("log.id as id,u.name as userName,log.log_time_str as logTime,log.log_content as logContent").
//		Joins("inner join hndz_user as u on log.user_id = u.id")
//	if pageSupport.Keyword != "" {
//		query = query.Where("u.name like ?", "%"+pageSupport.Keyword+"%")
//	}
//	location, err := time.LoadLocation("Local")
//	logger.HasError(err, "日期时间设置异常！")
//	if pageSupport.StartTime != "" {
//		startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", pageSupport.StartTime, location)
//		query = query.Where("log.log_time >= ?", startTime.Unix())
//	}
//	if pageSupport.EndTime != "" {
//		endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", pageSupport.EndTime, location)
//		query = query.Where("log.log_time <= ?", endTime.Unix())
//	}
//	err = query.Order("log.id desc").Count(&pageSupport.TotalCount).
//		Limit(pageSupport.PageSize).
//		Offset(pageSupport.GetOffSet()).
//		Scan(&OperationLogListStructList).Error
//	if !logger.HasError(err, "数据库查询错误") {
//		pageSupport.SetData(OperationLogListStructList)
//	}
//	return *pageSupport
//}
